/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.meibang.task.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.DateUtils;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.cache.RemoveCacheUtil;
import cn.apestar.modules.meibang.task.entity.Task;
import cn.apestar.modules.meibang.task.enums.TaskAdvertTypeEnum;
import cn.apestar.modules.meibang.task.enums.TaskStateEnum;
import cn.apestar.modules.meibang.task.service.TaskService;

import com.alibaba.fastjson.JSON;

/**
 * taskController
 * @author task
 * @version 2017-06-19
 */
@Controller
@RequestMapping(value = "${adminPath}/task")
public class TaskController extends BaseController {

    @Autowired
    private TaskService taskService;

    @ModelAttribute
    public Task get(@RequestParam(required = false) String id) {
        Task entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = taskService.get(id);
        }
        if (entity == null) {
            entity = new Task();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(Task task, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Task> page = taskService.findPage(new Page<Task>(request, response), task);

        System.out.println(page);

        model.addAttribute("page", page);
        return "modules/meibang/task/taskList";
    }

    @RequestMapping(value = "form")
    public String form(Task task, Model model) {
        model.addAttribute("task", task);
        return "modules/meibang/task/taskForm";
    }

    @ResponseBody
    @RequestMapping(value = "fetchPath")
    public Object fetchPath(HttpServletRequest request, String url) {

        String str = RemoveCacheUtil.getFecthCache(url, Global.getAppType());

        return getSuccessMessageObject("success", str);

    }

    @RequestMapping(value = "fetchList")
    public String fetchList(Task task, Model model) {

        model.addAttribute("task", task);

        return "modules/meibang/task/fetchPath";
    }

    @RequestMapping(value = "save")
    public String save(Task task, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, task)) {
            return form(task, model);
        }

        if (task.getLink() != null && task.getLink().length() > 0) {

            try {
                String str = RemoveCacheUtil.getFecthCache(task.getLink(), Global.getAppType());

                Map map = JSON.parseObject(str, HashMap.class);

                if (map.get("fetch_url") != null) {

                    System.out.println(map.get("fetch_url"));

                    task.setShowLink(map.get("fetch_url") + "");

                }

            } catch (Exception e) {

                task.setShowLink(null);

            }

        }

        if (task.getStartTime() == null) {

            task.setStartTime(DateUtils.getDate("yyyyMMdd"));

        }

        if (task.getEndTime() == null) {

            task.setEndTime(DateUtils.getDate("yyyyMMdd"));

        }

        task.setFlag(TaskStateEnum.TASK_STATE_YTF.getValue());

        task.setTaskType(TaskAdvertTypeEnum.TASK_CMS.getValue());

        taskService.save(task);
        addMessage(redirectAttributes, "保存成功");
        return "redirect:" + Global.getAdminPath() + "/task/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(Task task, RedirectAttributes redirectAttributes) {
        taskService.delete(task);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + Global.getAdminPath() + "/task/?repage";
    }

}